package com.blockmeta.bbs.minemodule.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.StatusBarUtils;
import com.blockmeta.bbs.businesslibrary.adapter.expandablelist.StickyHeaderLayout;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.R2;
import com.blockmeta.bbs.minemodule.adapter.CoinGotDetailAdapter;
import com.blockmeta.bbs.minemodule.pojo.CoinGotDetailPOJO;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 金币领取明细
 * 金币商城兑换记录
 * @author airthor
 * @date 2018 10 23
 */
public class CoinGotDetailActivity extends BaseActivity implements OnRefreshListener,OnLoadmoreListener {

    @BindView(R2.id.tv_title)
    TextView mTvTitle;
    @BindView(R2.id.btn_right)
    LinearLayout mBtnRight;
    @BindView(R2.id.rcv_got_detail)
    RecyclerView mRcvGotDetail;
    @BindView(R2.id.sticky_header)
    StickyHeaderLayout mStickyHeader;
    @BindView(R2.id.smart_refresh)
    SmartRefreshLayout mRefreshLayout;

    private ArrayList<CoinGotDetailPOJO> mGroups=new ArrayList<>();
    private CoinGotDetailAdapter mAdapter;
    private int mLastTime;

    private String mTitle = "领取明细";
    private String mBaseUrl = NetConstant.COIN_GOT_DETAIL;
    @Override
    protected boolean showTitle() {
        return false;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_coin_got_detail, null);
    }

    @Override
    public void setStatusBarColor() {
        StatusBarUtils.setDarkMode(this);
        StatusBarUtils.setWindowStatusBarColor(this,R.color.status_trans);
        StatusBarUtils.setWindowFullScreen(this);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadmoreListener(this);
        Intent intent = getIntent();
        if (intent!= null){
            mTitle = intent.getStringExtra("title");
            mBaseUrl = intent.getStringExtra("url");
        }

        mTvTitle.setText(mTitle);
        mBtnRight.setVisibility(View.INVISIBLE);

        mRcvGotDetail.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CoinGotDetailAdapter(this,mGroups);
        mRcvGotDetail.setAdapter(mAdapter);

        netWork(false);
    }

    private void netWork(final boolean isRefresh) {
        String url;
        if (mLastTime!=0&&!isRefresh){
            url =mBaseUrl+"?last_time="+mLastTime;
        }else {
            url =mBaseUrl;
        }
        OkHttpUtils.get()
                .addHeader("token", SharedPreferenceUtil.getString(this, Constant.TOKEN,null))
                .addHeader("uid", AccountHelper.getUid())
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if(mUnbinder == null || mRefreshLayout == null){
                            return;
                        }
                        if (mRefreshLayout.isRefreshing()){mRefreshLayout.finishRefresh();}
                        if (mRefreshLayout.isLoading()){mRefreshLayout.finishLoadmore(false);}
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if(mUnbinder == null || mRefreshLayout == null){
                            return;
                        }
                        if (mRefreshLayout.isRefreshing()){mRefreshLayout.finishRefresh();}
                        if (mRefreshLayout.isLoading()){mRefreshLayout.finishLoadmore();}
                        try{
                            CoinGotDetailPOJO[] array = new Gson().fromJson(response,CoinGotDetailPOJO[].class);
                            if (array.length == 0){
                                mRefreshLayout.finishLoadmoreWithNoMoreData();
                            }
                            if (isRefresh) {
                                mGroups.clear();
                                mLastTime = 0;
                            }
                            mGroups.addAll(Arrays.asList(array));

                            mAdapter.notifyDataChanged();
                            if (mGroups.size()>0) {
                                List<CoinGotDetailPOJO.ListBean> listBeans = mGroups.get(mGroups.size() - 1).list;
                                if (listBeans==null||listBeans.size()==0){return;}
                                if (mLastTime == listBeans.get(listBeans.size() - 1).time){
                                    mRefreshLayout.finishLoadmoreWithNoMoreData();
                                }else {
                                    mLastTime = listBeans.get(listBeans.size() - 1).time;
                                }
                            }
                        }catch (Exception e){

                        }
                    }
                });
    }

    @OnClick(R2.id.btn_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        netWork(true);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        netWork(false);
    }
}
